package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	. "web_api_gin/controller"
	"web_api_gin/core"
	"web_api_gin/dao"
	"web_api_gin/dto"
	"web_api_gin/entity"
)

var SysMenuGroupApi = SysMenuGroupController{}

type SysMenuGroupController struct {
}

//菜单分组列表
func (c *SysMenuGroupController) Index(ctx *gin.Context) {
	var searchParams dto.SysMenuGroupGetListRequest
	if err := ctx.ShouldBind(&searchParams); err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, core.ValidatorTranslateError(err))
		return
	}
	menuGroupList, total, err := dao.SysMenuGroup.GetMenuGroupList(ctx, &searchParams)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, err.Error())
		return
	}
	Success(ctx, gin.H{
		"list":  menuGroupList,
		"total": total,
	})
}

//保存分组数据
func (c *SysMenuGroupController) Save(ctx *gin.Context) {
	var form *entity.SysMenuGroup
	if err := ctx.ShouldBind(&form); err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, core.ValidatorTranslateError(err))
		return
	}
	//设置表单数据，方便保存日志
	ctx.Set("formData", form)
	rows, err := dao.SysMenuGroup.SaveMenuGroup(ctx, form)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, err.Error())
		return
	}
	Success(ctx, gin.H{
		"rows": rows,
	})
}

//删除
func (c *SysMenuGroupController) Destroy(ctx *gin.Context) {
	var form *dto.SysMenuGroupFormDestroy
	if err := ctx.ShouldBind(&form); err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, core.ValidatorTranslateError(err))
		return
	}
	//设置表单数据，方便保存日志
	ctx.Set("formData", form)
	rows, err := dao.SysMenuGroup.DelMenuGroup(ctx, form.GroupIds)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, err.Error())
		return
	}
	Success(ctx, gin.H{
		"rows": rows,
	})
}
